package ipc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBLogger {

	public String dbname;
	private Connection c = null;
	private Statement stmt = null;
	private boolean stmtdone = true;
	

	public DBLogger(String dbname) {
		super();
		this.dbname = dbname;
	}
	
	//connect
	public void connect() throws ClassNotFoundException, SQLException{
        Class.forName("org.hsqldb.jdbcDriver" );
		c = DriverManager.getConnection("jdbc:hsqldb:hsql://kipnuk.eecs.oregonstate.edu/"+dbname, "sa", "");
	}
	
	//do query
	public ResultSet doQuery(String sql) throws SQLException{
		Statement stamt;
		stamt = c.createStatement();
		return stamt.executeQuery(sql);
	}
	
	//execute update
	public int executeUpdate(String sql) throws SQLException{
		Statement stamt;
		stamt = c.createStatement();
		return stamt.executeUpdate(sql);
	}
	
	//execute batch
	public ResultSet execute() throws SQLException{
		stmt.executeBatch();
		ResultSet ret = stmt.getResultSet();
		stmtdone = true;
		return ret;
	}
	
	
	//add to batch
	public void addToBatch(String query) throws SQLException{
		
		if( stmtdone ){
			stmt = c.createStatement();
			stmtdone = false;
		}
		stmt.addBatch(query);
	}
	
	//close
	public void close() throws SQLException{
		c.commit();
		c.close();
	}
	
	public static void main(String[] args) throws SQLException, ClassNotFoundException {
		DBLogger dbl = new DBLogger("concvi");
		
		dbl.connect();
		
		dbl.executeUpdate("INSERT INTO RUNINFO (RUNID, FNTYPE, INSTNAME) VALUES (0, 'AADD-CONC', 'TEST')");
		
		dbl.close();
	}
}
